package com.sanjiahantang.youjiarole.module.common.views.impl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.module.common.beans.ConditionBean;
import com.sanjiahantang.youjiarole.module.common.presenters.SelectorPresenter;
import com.sanjiahantang.youjiarole.module.common.views.SelectorView;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by zy on 2017/10/18.
 * 选择器页
 */

public class SelectorActivity extends BaseMvpActivity<SelectorView,SelectorPresenter> implements SelectorView, View.OnClickListener {

    private ListView listView;
    private boolean loadLocalDic;
    private QuickAdapter<ConditionBean.SelectorData> listAdapter;
    private ConditionBean.SelectorData currentItem = null;
    private String initIndex;

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public SelectorPresenter initPresenter() {
        return new SelectorPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_common_list);
        TextView title = (TextView) findViewById(R.id.title);
        String strTitle = getIntent().getStringExtra(KEY_TITLE);
        title.setText(strTitle);
        findViewById(R.id.fl_Left).setOnClickListener(this);
        findViewById(R.id.flIcon3).setVisibility(View.VISIBLE);
        findViewById(R.id.flIcon3).setOnClickListener(this);
        listView = (ListView) findViewById(R.id.listView);
        initIndex = getIntent().getStringExtra(SelectorView.KEY_INDEX);


        presenter.setOnLoadListener(new OnLoadListener() {
            @Override
            public void loadData(List<ConditionBean.SelectorData> data) {
                listAdapter.clear();
                listAdapter.addAll(data);
                if (data.size() > 0){
                    currentItem = data.get(0);
                }
            }
        });

        listAdapter = new QuickAdapter<ConditionBean.SelectorData>($this,R.layout.item_selector_text) {
            @Override
            protected void convert(BaseAdapterHelper helper, final ConditionBean.SelectorData item) {
                helper.setText(R.id.text1,item.getDicName());
                helper.setTag(R.id.text1,item.getDicValue());
                if (item.isChecked()){
                    helper.setVisible(R.id.ivCheck,true);
                }else{
                    helper.setVisible(R.id.ivCheck,false);
                }
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(currentItem != null){
                            currentItem.setChecked(false);
                        }
                        item.setChecked(false);
                        item.setChecked(true);
                        listAdapter.notifyDataSetChanged();
                        currentItem = item;
                    }
                });
            }
        };

        listView.setAdapter(listAdapter);
    }

    @Override
    public void initData() {
        if (getIntent().getBundleExtra(KEY_PARAM) == null){
            loadLocalDic = false;
        }else {
            loadLocalDic = true;
            List<ConditionBean.SelectorData> data = (List<ConditionBean.SelectorData>) getIntent().getBundleExtra(KEY_PARAM).get(KEY_PARAM);
            listAdapter.clear();
            listAdapter.addAll(data);
            int index = 0;
            for (int i = 0,max = data.size();i < max;i++){
                if (data.get(i).getDicValue().equals(initIndex)){
                    index = i;
                    break;
                }
            }
            if (data.size() > index){
                currentItem = data.get(index);
                currentItem.setChecked(true);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                finish();
                break;
            case R.id.flIcon3:
                Intent intent = new Intent();
                intent.putExtra(KEY_PARAM,currentItem);
                setResult(RESULT_OK,intent);
                finish();
                break;
        }
    }

    public static void startActivity(Activity context, ArrayList<ConditionBean.SelectorData> list,String title,String index,int requestCode){
        if (list == null){
            return;
        }
        Intent intent = new Intent(context, SelectorActivity.class);
        Bundle b = new Bundle();
        b.putSerializable(KEY_PARAM,list);
        intent.putExtra(KEY_PARAM,b);
        intent.putExtra(KEY_TITLE,title);
        if (!TextUtils.isEmpty(index)){
            intent.putExtra(KEY_INDEX,index);
        }
        context.startActivityForResult(intent,requestCode);
    }

    public static void startActivity(Activity context, ArrayList<ConditionBean.SelectorData> list,String title,int requestCode){
        SelectorActivity.startActivity(context,list,title,null,requestCode);
    }
}
